package com.aurora.oa.message.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aurora.oa.R;
import com.aurora.oa.message.bean.ContactsGroupBean;
import com.aurora.oa.message.bean.ContactsInfo;
import com.aurora.oa.message.bean.ContactsListBean;
import com.aurora.oa.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Class: ListAdapter
 * @Description: 数据适配器
 */
public class ContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_GROUP = 0;
    private static final int TYPE_CONTACTS = 1;
    private boolean isNeedPullRefresh;
    private boolean hasMoreData=true;
    private Context context;
    //适配器数据
    private List<ContactsListBean> mDatas;
    private List<ContactsGroupBean> groupData;
    private int needShowItemCount = 0;
    private OnItemClickListener mOnItemClickListener;
    private OnItemCheckChangeListener itemCheckChangeListener;

    public ContactsAdapter(Context context, List<ContactsGroupBean> groupData, boolean isNeedPullRefresh) {
       this.groupData =groupData;
        this.context = context;
        this.isNeedPullRefresh = isNeedPullRefresh;
        initData();
    }

    private void initData() {
        mDatas = new ArrayList<>();
        for(int i =0;i<groupData.size();i++){
            //先把组信息填充进来
            ContactsListBean mycontactsBean = new ContactsListBean();
            mycontactsBean.isExpand =false;
            mycontactsBean.groupName =groupData.get(i).name;
            mycontactsBean.currentGroupPosition = i;
            mycontactsBean.type = TYPE_GROUP;
            mDatas.add(mycontactsBean);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
       return mDatas.get(position).type;
    }

    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int i) {
        switch(holder.getItemViewType()){
            case TYPE_GROUP:
                GroupViewHolder itemViewHolder =(GroupViewHolder) holder;
                if(mDatas.get(i).isExpand){
                    itemViewHolder.iv_arrow.setImageResource(R.mipmap.ico_list_down_arrow);
                }else{
                    itemViewHolder.iv_arrow.setImageResource(R.mipmap.ico_list_right_arrow);
                }
                itemViewHolder.tv_groupname.setText(mDatas.get(i).groupName);
                itemViewHolder.itemView.setTag(itemViewHolder.getLayoutPosition());
                itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       // mOnItemClickListener.onItemClick(view,(int)view.getTag());
                        List<ContactsInfo> contactsInfoList =groupData.get(mDatas.get(i).currentGroupPosition).contactsList;
                        if(mDatas.get(i).isExpand){
                            //删除组下的数据
                            for(int j=0;j<contactsInfoList.size();j++){
                                mDatas.remove(i+1);

                                   notifyItemRemoved(i+1);
                                if(i!=mDatas.size()-1){
                                    notifyItemRangeChanged(i+1,mDatas.size()-i-1);
                                }
                            }
                            mDatas.get(i).isExpand =false;
                             notifyItemChanged(i);
                           // notifyDataSetChanged();


                        }else{
                            //添加数据
                            for(int j =0;j<contactsInfoList.size();j++){
                                ContactsListBean mContactsBean =new ContactsListBean();
                                mContactsBean.type = TYPE_CONTACTS;
                                mContactsBean.currentGroupPosition = mDatas.get(i).currentGroupPosition;
                                mContactsBean.contactsInfo =contactsInfoList.get(j);
                                mDatas.add(i+1,mContactsBean);
                                notifyItemInserted(i+1);
                                 if(i!=mDatas.size()-1){
                                    notifyItemRangeChanged(i+1,mDatas.size()-i-1);
                                }

                            }
                            mDatas.get(i).isExpand =true;
                            notifyItemChanged(i);
                           // notifyDataSetChanged();

                        }

                    }
                });

                break;
            case TYPE_CONTACTS:
                ContactsViewHolder contactsViewHolder =(ContactsViewHolder) holder;
                contactsViewHolder.cb_ischeck.setBackgroundResource(mDatas.get(i).contactsInfo.isCheck==true?R.mipmap.check_h:R.mipmap.check_nor);
                contactsViewHolder.tv_name.setText(mDatas.get(i).contactsInfo.username);
                contactsViewHolder.tv_phone.setText(mDatas.get(i).contactsInfo.phone);
                contactsViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if(mDatas.get(i).contactsInfo.isCheck) {
                            mDatas.get(i).contactsInfo.isCheck=false;
                            notifyItemChanged(i);
                        }else{
                            mDatas.get(i).contactsInfo.isCheck=true;
                            notifyItemChanged(i);
                        }
                    }
                });
                break;
        }

    }

    /*public void doOnGroupClick(int position){
         //先得到组下联系人的信息
                        List<ContactsInfo> contactsInfoList =groupData.get(mDatas.get(position).currentGroupPosition).contactsInfos;
                        if(mDatas.get(position).isExpand){
                            //删除组下的数据
                            for(int j=0;j<contactsInfoList.size();j++){
                                mDatas.remove(position+1);
                            *//*   notifyItemRemoved(position+1);
                                if(position!=mDatas.size()-1){
                                    notifyItemRangeChanged(position+1,mDatas.size()-position-1);
                                }*//*
                            }
                            mDatas.get(position).isExpand =false;
                           // notifyItemChanged(position);
                            notifyDataSetChanged();


                        }else{
                            //添加数据
                            for(int j =0;j<contactsInfoList.size();j++){
                                ContactsListBean mContactsBean =new ContactsListBean();
                                mContactsBean.type = TYPE_CONTACTS;
                                mContactsBean.currentGroupPosition = mDatas.get(position).currentGroupPosition;
                                mContactsBean.contactsInfo =contactsInfoList.get(j);
                                mDatas.add(position+1,mContactsBean);
                               *//* notifyItemInserted(position+1);
                                if(position!=mDatas.size()-1){
                                    notifyItemRangeChanged(position+1,mDatas.size()-position-1);
                                }*//*

                            }
                            mDatas.get(position).isExpand =true;
                           // notifyItemChanged(position);
                            notifyDataSetChanged();

                        }
    }*/

    public void doOnCheckChange(int position,boolean checked){
        mDatas.get(position).contactsInfo.isCheck=checked;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        /**
         * 使用RecyclerView，ViewHolder是可以复用的。这根使用ListView的VIewHolder复用是一样的
         * ViewHolder创建的个数好像是可见item的个数+3
         */

        if (viewType == TYPE_GROUP) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_contacts_group, parent,
                    false);
            return new GroupViewHolder(view);
        } else if (viewType == TYPE_CONTACTS) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_contacts, parent,
                    false);
            return new ContactsViewHolder(view);
        }
        return null;
    }



    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    public void setOnItemCheckChangeListener(OnItemCheckChangeListener onItemCheckChangeListener) {
        this.itemCheckChangeListener = onItemCheckChangeListener;
    }

    public String getCheckedUserIDs() {
        String IDs;
        List<String> userIDs = new ArrayList<>();
        for(int i=0;i<groupData.size();i++){
            List<ContactsInfo> contactsInfoList =groupData.get(i).contactsList;
            for(int j=0;j<contactsInfoList.size();j++){
                ContactsInfo contactsInfo =contactsInfoList.get(j);
                if(contactsInfo.isCheck){
                    userIDs.add(contactsInfo.userid);
                }
            }
        }
        IDs = StringUtils.getStringFromList(userIDs);
        return IDs;
    }

    public Map<String, Object> getSelectedContactsAndIDs() {
        Map<String ,Object> contactsAndIDs =new HashMap<>();
        List<ContactsInfo> selectedcontactsInfoList = new ArrayList<>();
        String IDs;
        List<String> userIDs = new ArrayList<>();
        for(int i=0;i<groupData.size();i++){
            List<ContactsInfo> contactsInfoList =groupData.get(i).contactsList;
            for(int j=0;j<contactsInfoList.size();j++){
                ContactsInfo contactsInfo =contactsInfoList.get(j);
                if(contactsInfo.isCheck){
                    selectedcontactsInfoList.add(contactsInfo);
                    userIDs.add(contactsInfo.userid);
                }
            }
        }
        IDs = StringUtils.getStringFromList(userIDs);
        contactsAndIDs.put("contactsList",selectedcontactsInfoList);
        contactsAndIDs.put("ids",IDs);
        return  contactsAndIDs;
    }


    /**
     * 处理item的点击事件和长按事件
     */
    public interface OnItemClickListener {
        public void onItemClick( View view, int position);
        public void onItemLongClick(View view, int position);
    }

    public interface OnItemCheckChangeListener{
        public void onItemCheckChange(View view,int position,boolean checked );
    }

    static class GroupViewHolder extends RecyclerView.ViewHolder {


        private ImageView iv_arrow;
        private TextView tv_groupname;

        public GroupViewHolder(View itemView) {
            super(itemView);
            iv_arrow = (ImageView) itemView.findViewById(R.id.iv_arrow);
            tv_groupname = (TextView) itemView.findViewById(R.id.tv_groupname);
        }
    }

    static class ContactsViewHolder extends RecyclerView.ViewHolder {
        private ImageView cb_ischeck ;
        private TextView tv_name;
        private TextView tv_phone;

        public ContactsViewHolder(View view) {
            super(view);
            cb_ischeck = (ImageView) view.findViewById(R.id.cb_ischeck);
            tv_name = (TextView)view.findViewById(R.id.tv_title);
            tv_phone = (TextView)view.findViewById(R.id.tv_phone);
        }
    }

}